package com.zijie.shop.order.api.api;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zijie.core.util.JsonResult;
import com.zijie.order.model.Order;
import com.zijie.order.model.search.OrderSearchBean;
import com.zijie.order.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/v1/order")
public class OrderApi {
    private OrderService orderService;
    @Autowired
    public void setOrderService(OrderService orderService) {
        this.orderService = orderService;
    }
    @GetMapping
//查询所有信息
    public ResponseEntity<JsonResult<?>> findAll(@RequestParam(defaultValue = "1")Integer pageNo,
                                                 @RequestParam(defaultValue = "10")Integer pageSize,
                                                 OrderSearchBean osb){
        Page<Order> page = Page.of(pageNo, pageSize);
        page=orderService.findAll(page, osb);
        return ResponseEntity.ok(JsonResult.success(page));
    }

    @DeleteMapping("/deleteById/{id}")
    public ResponseEntity<JsonResult<?>> deleteById(@PathVariable Integer id){
        return ResponseEntity.ok(JsonResult.success(orderService.deleteById(id)));
    }
    //    删除多个
    @DeleteMapping("/delete")
    public ResponseEntity<JsonResult<?>> deleteByIds(@RequestParam("ids") List<Integer> ids) {
        return ResponseEntity.ok(JsonResult.success(orderService.deleteByIds(ids)));
    }
    @GetMapping("/findById/{id}")
    public ResponseEntity<JsonResult<?>> findById(@PathVariable Integer id) {
        Order order = orderService.findById(id);
        if (order == null) {
            return ResponseEntity.ok(JsonResult.error("未找到该订单"));
        }
        return ResponseEntity.ok(JsonResult.success(order));
    }

    //
    @PutMapping("/fahuo/{id}")
    public ResponseEntity<JsonResult<?>> fahuo(@PathVariable Integer id) {
        boolean fahuoResult = orderService.fahuo(id);
        if(fahuoResult){
            return ResponseEntity.ok(JsonResult.success("发货成功"));
        }else {
            return ResponseEntity.ok(JsonResult.error("发货失败"));
        }

    }
    @PutMapping("/songda/{id}")
    public ResponseEntity<JsonResult<?>> songda(@PathVariable Integer id) {
        boolean fahuoResult = orderService.songda(id);
        if(fahuoResult){
            return ResponseEntity.ok(JsonResult.success("送达成功"));
        }else {
            return ResponseEntity.ok(JsonResult.error("送达失败"));
        }
    }
    //新增
    @PostMapping("/save")
    public ResponseEntity<JsonResult<?>> save(@RequestBody Order order) {
        boolean save = orderService.save(order);
        if (save) {
            return ResponseEntity.ok(JsonResult.success(order));
        } else {
            return ResponseEntity.ok(JsonResult.error("保存失败"));
        }
    }
    //修改
    @PutMapping("/update")
    public ResponseEntity<JsonResult<?>> update(@RequestBody Order order) {
        boolean update = orderService.update(order);
        if (update) {
            return ResponseEntity.ok(JsonResult.success(order));
        } else {
            return ResponseEntity.ok(JsonResult.error("修改失败"));
        }
    }
    @GetMapping("/all")
    public ResponseEntity<JsonResult<?>> findAll2(OrderSearchBean osb){

        List<Order> all = orderService.findAll2(osb);
        return ResponseEntity.ok(JsonResult.success(all));
    }

}